package com.ztstkj.lzservices.retrofits

import com.blankj.utilcode.util.AppUtils
import okhttp3.ConnectionPool
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

object Api {
    private var baseUrl: String = "https://api.ztstkj.cn"
    private const val CONNECT_TIMEOUT: Long=60
    private const val READ_TIMEOUT: Long=60
    private const val WRITE_TIMEOUT: Long=60
    private val okHttpClient: OkHttpClient by lazy {
        OkHttpClient.Builder()
            .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
            .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
            .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
            .connectionPool(ConnectionPool(10,60 ,TimeUnit.SECONDS)) //自定义连接池最大空闲连接数和等待时间大小，否则默认最大5个空闲连接
            .addInterceptor(HttpLoggingInterceptor().apply {
                level = if(AppUtils.isAppDebug()){
                    HttpLoggingInterceptor.Level.BODY
                }else{
                    HttpLoggingInterceptor.Level.BASIC
                }
            })
            //.addInterceptor(LogInterceptor())
            //.addInterceptor(HeaderInterceptor())
            //.addInterceptor(MockHttpInterceptor())
            .build()
    }


    private val api by lazy {
        val retrofit = Retrofit.Builder()
            .baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            //.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .client(okHttpClient)
            .build()
        retrofit.create(IApi::class.java)
    }

    fun get(): IApi {
        return api
    }
}